package com.kaussoft.lolblog.dao.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.kaussoft.lolblog.dao.BaseDAO;

public class BaseDAOImpl extends HibernateDaoSupport implements BaseDAO {

	public void delete(Object object) {
		getHibernateTemplate().delete(object);
	}

	public void deleteAll(Collection collection) {
		getHibernateTemplate().deleteAll(collection);
	}

	public Object load(Class clazz, Serializable id) {
		return getHibernateTemplate().load(clazz, id);
	}

	public Object loadInitialize(Class clazz, Serializable id) {
		Object object = load(clazz, id);
		getHibernateTemplate().initialize(object);
		return object;
	}
	
	public List loadAll(Class clazz) {
		return getHibernateTemplate().loadAll(clazz);
	}
	
	public Object get(Class clazz, Serializable id) {
		return getHibernateTemplate().get(clazz, id);
	}

	public Object getInitialize(Class clazz, Serializable id) {
		Object object = get(clazz, id);
		getHibernateTemplate().initialize(object);
		return object;
	}

	public void merge(Object object) {
		getHibernateTemplate().merge(object);
	}

	public void refresh(Object object) {
		getHibernateTemplate().refresh(object);
	}

	public void save(Object object) {
		getHibernateTemplate().save(object);
	}

	public void saveOrUpdate(Object object) {
		getHibernateTemplate().saveOrUpdate(object);
	}

	public void saveOrUpdateAll(Collection collection) {
		getHibernateTemplate().saveOrUpdateAll(collection);
	}

	public void update(Object object) {
		try {
			getHibernateTemplate().update(object);
        } catch (Exception e) {
            merge(object);
        }
	}

	public List findListByHql(String hql) {
		return findListByHql(hql, null);
	}

	public List findListByHql(String hql, Object value) {
		return findListByHql(hql, new Object[] {value});
	}

	public List findListByHql(final String hql, final Object[] values) {
		return getHibernateTemplate().executeFind(
	            (new HibernateCallback() {
	                public Object doInHibernate(Session session) {
	                    Query queryObject = session.createQuery(hql);

	                    if (values != null) {
	                        for (int i = 0; i < values.length; i++) {
	                            queryObject.setParameter(i, values[i]);
	                        }
	                    }

	                    return queryObject.list();
	                }
	            })
	        );
	}

	public Object findObjectByHql(String hql) {
		return findObjectByHql(hql, null);
	}

	public Object findObjectByHql(String hql, Object value) {
		return findObjectByHql(hql, new Object[] {value});
	}

	public Object findObjectByHql(final String hql, final Object[] values) {
		List list = findListByHql(hql, values);

        if (list.size() > 0) {
            return list.get(0);
        }

        return null;
	}

}
